package edu.bupt.c1array;

public class T18GasStation {
    public int canCompleteCircuit(int[] gas, int[] cost) {

        int start = 0;
        int tempsum = 0;
        int tempcost = 0;
        int allsum = 0;
        int allcost = 0;
        for (int i=0;i<gas.length;i++){
            tempsum += gas[i];
            allsum += gas[i];
            tempcost += cost[i];
            allcost += cost[i];

            if (tempsum < tempcost){
                start = i+1;
                tempsum=0;
                tempcost=0;
            }
        }

        if (allsum < allcost){
            return -1;
        }else {
            return start;
        }

    }
}
